/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.gateway.mqtt.client.sample.service;

import lombok.extern.slf4j.Slf4j;
import net.dreamlu.iot.mqtt.client.noear.MqttClientTemplate;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.nio.charset.StandardCharsets;

/**
 * <b>(MqttClientSampleService)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/7/19
 */
@Slf4j
@Component
public class MqttClientSampleService {
    @Inject
    private MqttClientTemplate mqttClientTemplate;

    public boolean publish(String body) {
        mqttClientTemplate.publish("/test/client", body.getBytes(StandardCharsets.UTF_8));
        return true;
    }

    public boolean sub() {
        mqttClientTemplate.subQos2("/test/#", (context, topic, message, payload) -> {
            log.info(topic + '\t' + payload);
        });
        return true;
    }

}